package com.peanutplan.blog.controller.admin;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.peanutplan.blog.controller.BaseController;
import com.peanutplan.blog.dao.EsHotWordMapper;
import com.peanutplan.blog.exception.TipException;
import com.peanutplan.blog.model.bo.RestResponseBo;
import com.peanutplan.blog.model.vo.EsHotWord;

import tk.mybatis.mapper.entity.Example;

/**
 * <p>Title: EsHotWordsController</p>
 * <p>Description: </p>
 * @author motisy
 * @date 2020年3月11日
 */
@Controller
@RequestMapping("admin/esHotWord")
public class EsHotWordsController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(EsHotWordsController.class);

    @Resource
    private EsHotWordMapper esHotWordMapper;

    @PostMapping(value = "save")
    @ResponseBody
    @Transactional(rollbackFor = TipException.class)
    public RestResponseBo<?> save(@RequestParam String esHotWord) {
        try {
        	Example example = new Example(EsHotWord.class);
        	example.createCriteria().andEqualTo("hotWord", esHotWord);
        	long count = esHotWordMapper.selectCountByExample(example);
        	if(count>0) {
        		return RestResponseBo.fail("已经存在热词【"+esHotWord+"】，请不要重复添加！");
        	} 
        	
        	EsHotWord hotWord = new EsHotWord();
        	hotWord.setCreated(new Date().getTime());
        	hotWord.setHotWord(esHotWord);
        	esHotWordMapper.insert(hotWord);
        } catch (Exception e) {
            String msg = "保存失败";
            if (e instanceof TipException) {
                msg = e.getMessage();
            } else {
                LOGGER.error(msg, e);
            }
            return RestResponseBo.fail(msg);
        }
        return RestResponseBo.ok();
    }
    
    /**
     * 评论列表
     * @param page
     * @param limit
     * @param request
     * @return
     */
    @GetMapping(value = "")
    public String index(@RequestParam(value = "page", defaultValue = "1") int page,
                        @RequestParam(value = "limit", defaultValue = "10") int limit, HttpServletRequest request) {
        PageHelper.startPage(page, limit);
        List<EsHotWord> ws = esHotWordMapper.selectAll();
    	PageInfo<EsHotWord> p = new PageInfo<>(ws);
        request.setAttribute("esHotWords", p);
        return "admin/esHotWords_list";
    }

    /**
     * 删除一条评论
     * @param coid
     * @return
     */
    @PostMapping(value = "delete")
    @ResponseBody
    @Transactional(rollbackFor = TipException.class)
    public RestResponseBo<?> delete(@RequestParam Long id) {
        try {
            esHotWordMapper.deleteByPrimaryKey(id);
        } catch (Exception e) {
            String msg = "删除失败";
            if (e instanceof TipException) {
                msg = e.getMessage();
            } else {
                LOGGER.error(msg, e);
            }
            return RestResponseBo.fail(msg);
        }
        return RestResponseBo.ok();
    }

}
